/**
 * Copyright (c) 2016-2019 人人开源 All rights reserved.
 * <p>
 * https://www.renren.io
 * <p>
 * 版权所有，侵权必究！
 */

package com.atguigu.common.utils;

import com.atguigu.common.exception.BizErrorCode;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpStatus;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * 返回数据
 *
 * @author Mark sunlightcs@gmail.com
 */
@Slf4j
public class R extends HashMap<String, Object> {
    //
    private static final long serialVersionUID = 1L;

    private final ObjectMapper objectMapper = new ObjectMapper();


//    private final TypeReference<T> typeReference = new TypeReference<T>() {};

//    @SneakyThrows
//    public <T> T getData(TypeReference<T> typeReference){
//        Object data = get("data");
//        String s = objectMapper.writeValueAsString(data);
//        T t = objectMapper.readValue(s, typeReference);
//        return t;
//    }

    @SneakyThrows
    public <T> T getData(String name, TypeReference<T> typeReference) {
        Object data = get(name);
        String s = objectMapper.writeValueAsString(data);
//        log.info("data: {}", s);
        return objectMapper.readValue(s, typeReference);
    }

    @SneakyThrows
    public <T> T getData(TypeReference<T> typeReference) {
        return getData("data", typeReference);
    }

    public <T> R setData(T data) {
        put("data", data);
        return this;
    }

    public String getMessage() {
        return this.getOrDefault("msg", "").toString();
    }


//    public T getPayload() {
//        try {
//        if (this.containsKey("data")) {
//
//            String data = objectMapper.writeValueAsString(this.get("data"));
//
//            Class<?> v = payload.getClass();
//            return objectMapper.readValue(data,new TypeReference<>(){});
//        }
//        if (this.containsKey("page")) {
//            String data = objectMapper.writeValueAsString(this.get("page"));
//
//            return objectMapper.readValue(data,new TypeReference<T>(){});
//        }
//        return null;
//        } catch (IOException e) {
//            throw new RuntimeException(e);
//        }
//    }


//    @Getter
//    @Setter
//    private T payload;

//    public R<T> setPayload(T payload) {
//        this.payload = payload;
////        this.put("msg", msg);
//        return this;
//    }
//
//    public T getPayload() {
////        if (this.containsKey("data")) {
////            return (T) this.get("data");
////        }
////        if (this.containsKey("page")) {
////            return (T) this.get("page");
////        }
//        return payload;
//    }

    public R() {
        put("code", 0);
        put("msg", "success");
    }

    public static R error() {
        return error(HttpStatus.SC_INTERNAL_SERVER_ERROR, "未知异常，请联系管理员");
    }

    public static R error(String msg) {
        return error(HttpStatus.SC_INTERNAL_SERVER_ERROR, msg);
    }

    public static R error(BizErrorCode bizErrorCode) {
        return error(bizErrorCode.getCode(), bizErrorCode.getMessage());
    }

    public static R error(int code, String msg) {
        R r = new R();
        r.put("code", code);
        r.put("msg", msg);
        return r;
    }

    public static R ok(String msg) {
        R r = new R();
        r.put("msg", msg);
        return r;
    }

    public static R ok(Map<String, Object> map) {
        R r = new R();
        r.putAll(map);
        return r;
    }

    public static R ok() {
        return new R();
    }

    public R put(String key, Object value) {
        super.put(key, value);
        return this;
    }


    public Boolean isOk() {
        return Objects.equals(this.get("code"), 0);
    }
}
